package com.boot.mapper;

import com.boot.pojo.Account;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

/**
* @author liyan
* @description 针对表【account】的数据库操作Mapper
* @createDate 2025-05-08 09:42:12
* @Entity com.boot.pojo.Account
*/
@Mapper
public interface AccountMapper{
    @Update("update account set balance = balance - #{amount} where user_id = #{userId}")
    int deductBalance(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    @Update("update account set balance = balance + #{amount} where user_id = #{userId}")
    int compensateBalance(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    @Select("select balance from account where user_id = #{userId}")
    BigDecimal getBalanceByUserId(@Param("userId") Long userId);

    @Update("update account set compensating = #{status} where user_id = #{userId}")
    void setCompensating(@Param("userId")Long userID, @Param("status") Boolean isCompensating);

    @Select("select user_id,compensating from account for update")
    Account selectByUserIdForUpdate(@Param("userId") Long userId);

    @Update("update account set balance = balance + #{amount},compensating = true where user_id = #{userId} and compensating = false")
    void markAndCompensate(@Param("userId") Long userId, @Param("amount") BigDecimal amount);
}




